﻿using AcademySystem.Service;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting.Web;
using AcademySystem.DAL.Helpers;
using AcademySystem.DAL;
using System.Collections.Generic;
using AcademySystem.Service.DTO;
using System.Net;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Json;

namespace AcademySystem.Service.Tests
{
    
    
    /// <summary>
    ///This is a test class for AcademySystemServiceTest and is intended
    ///to contain all AcademySystemServiceTest Unit Tests
    ///</summary>
    [TestClass()]
    public class AcademySystemServiceTest
    {
        public const string PORT = "10525";
        public const string SERVICE_NAME = "TwitterService";
        public readonly string ServiceBaseUrl = "http://localhost:" + PORT + "/" + SERVICE_NAME + ".svc/";

        [TestMethod()]
        public void AuthenticateTest()
        {
            Assert.Inconclusive("Verify the correctness of this test method.");
        }

        [TestMethod()]
        public void ConvertCourcesTest()
        {
            Assert.Inconclusive("Verify the correctness of this test method.");
        }

        [TestMethod()]
        public void ConvertSubscriptionsTest()
        {
            Assert.Inconclusive("Verify the correctness of this test method.");
        }

        [TestMethod()]
        public void GetAllCoursesTest()
        {
            Assert.Inconclusive("Verify the correctness of this test method.");
        }

        [TestMethod()]
        public void GetCoursesTest()
        {
            Assert.Inconclusive("Verify the correctness of this test method.");
        }

        [TestMethod()]
        public void GetTopCoursesTest()
        {
            Assert.Inconclusive("Verify the correctness of this test method.");
        }

        [TestMethod()]
        public void GetUserCoursesTest()
        {
            Assert.Inconclusive("Verify the correctness of this test method.");
        }

        [TestMethod()]
        public void GetUserSubscriptionsTest()
        {
            Assert.Inconclusive("Verify the correctness of this test method.");
        }

        [TestMethod()]
        public void SubscribeTest()
        {
            Assert.Inconclusive("A method that does not return a value cannot be verified.");
        }

        #region Helper methods

        private string InvokeServiceHttpGet(string relativeUrl)
        {
            WebClient webClient = new WebClient();
            byte[] serviceResponse = webClient.DownloadData(ServiceBaseUrl + relativeUrl);
            string serviceResponseStr = Encoding.UTF8.GetString(serviceResponse);
            return serviceResponseStr;
        }

        private string InvokeServiceHttpPost(string relativeUrl, object objectToPost)
        {
            string url = ServiceBaseUrl + relativeUrl;
            HttpWebRequest httpRequest = WebRequest.Create(url) as HttpWebRequest;
            httpRequest.Method = "POST";
            httpRequest.ContentType = "application/json";
            Stream httpRequestStream = httpRequest.GetRequestStream();
            using (httpRequestStream)
            {
                string jsonToPost = ConvertToJSON(objectToPost);
                byte[] bytesToPost = UTF8Encoding.UTF8.GetBytes(jsonToPost);
                httpRequestStream.Write(bytesToPost, 0, bytesToPost.Length);
            }
            HttpWebResponse httpResponse = httpRequest.GetResponse() as HttpWebResponse;
            StreamReader httpResponseReader = new StreamReader(httpResponse.GetResponseStream());
            using (httpResponseReader)
            {
                string httpResponseBody = httpResponseReader.ReadToEnd();
                return httpResponseBody;
            }
        }

        private string ConvertToJSON(object obj)
        {
            DataContractJsonSerializer jsonSerializer =
                new DataContractJsonSerializer(obj.GetType());
            MemoryStream jsonStream = new MemoryStream();
            jsonSerializer.WriteObject(jsonStream, obj);
            byte[] jsonData = jsonStream.ToArray();
            string jsonString = Encoding.UTF8.GetString(jsonData);
            return jsonString;
        }

        private T DeserializeJSON<T>(string json)
        {
            DataContractJsonSerializer jsonSerializer =
                new DataContractJsonSerializer(typeof(T));
            byte[] jsonBytes = Encoding.UTF8.GetBytes(json);
            MemoryStream jsonStream = new MemoryStream(jsonBytes);
            jsonStream.Position = 0;
            T deserializedTtem = (T)jsonSerializer.ReadObject(jsonStream);
            return deserializedTtem;
        }

        #endregion
    }
}
